package com.liangjie.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "log", urlPatterns = "/*")
public class LogFilter implements Filter {

	// 用于访问filter配置信息
	@SuppressWarnings("unused")
	private FilterConfig config = null;

	@Override
	public void destroy() {
		config = null;
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		long before = System.currentTimeMillis();
		System.out.println("开始过滤...");
		HttpServletRequest hrequest = (HttpServletRequest) arg0;

		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		arg2.doFilter(arg0, arg1);

		long after = System.currentTimeMillis();
		System.out.println("过滤结束");
		System.out.println("请求被定位到" + hrequest.getRequestURI() + ",所花时间为"
				+ (after - before) + "毫秒");
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
	}

}
